
package com.hlkj.pay.app.merchant.ext.impl.huifu.ext;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.merchant.ext.dto.order.ChannelPayOrderContext;
import com.hlkj.pay.app.merchant.ext.dto.order.ChannelPayOrderQueryContext;
import com.hlkj.pay.app.merchant.ext.dto.order.ChannelRefundPayOrderContext;
import com.hlkj.pay.app.merchant.ext.impl.huifu.HuiFuExtConstant;
import com.hlkj.pay.app.merchant.ext.impl.huifu.HuiFuScenePayOrderExtPt;
import com.hlkj.pay.app.merchant.ext.impl.huifu.dto.HuiFuAppChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.huifu.dto.HuiFuConfig;
import com.hlkj.pay.app.merchant.ext.impl.huifu.dto.V2TradeHostingPaymentPreorderRequest;
import com.hlkj.pay.app.merchant.ext.impl.huifu.handler.params.HuiFuMerchantAccessCompanyParamsHandle;
import com.hlkj.pay.app.merchant.ext.impl.huifu.handler.params.HuiFuPayOrderRequestParamsHandler;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import com.huifu.bspay.sdk.opps.client.BasePayClient;
import com.huifu.bspay.sdk.opps.core.BasePay;
import com.huifu.bspay.sdk.opps.core.config.MerConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * @author HlpayTeam
 * @date 2024/10/11 16:25
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = HuiFuExtConstant.HOSTING)
public class HuiFuHostIngPayOrderExtPt extends HuiFuSceneCommonPayOrder implements HuiFuScenePayOrderExtPt {

    @Override
    public void submitChannelPayOrder(ChannelPayOrderContext channelPayOrderContext) {
        ProviderChannelConfigDO providerChannelConfigDO = channelPayOrderContext.getProviderChannelConfigDO();
        String channelConfig = providerChannelConfigDO.getParamsConfig();
        // 配置转换
        HuiFuConfig huiFuConfig = JsonUtils.parseObject(channelConfig, HuiFuConfig.class);
        HuiFuAppChannelParams huiFuAppChannelParams = JsonUtils.parseObject(channelPayOrderContext.getMerchantAppChannelDO().getAppConfigJson(), HuiFuAppChannelParams.class);
        try {
            // 1. 数据初始化
            MerConfig merConfig = HuiFuMerchantAccessCompanyParamsHandle.merConfig(huiFuConfig);
            BasePay.initWithMerConfig(merConfig);
            V2TradeHostingPaymentPreorderRequest request = HuiFuPayOrderRequestParamsHandler.v2TradePaymentHostingRequest(channelPayOrderContext);
            if (!channelPayOrderContext.isProcessStatus()) {
                return;
            }
            // 汇付客户Id
            request.setHuifuId(huiFuAppChannelParams.getHuiFuId());
            log.info("斗拱提交APP支付订单请求信息response:{}", JsonUtils.toJsonString(request));
            // 3. 发起API调用
            Map<String, Object> response = BasePayClient.request(request, false);
            log.info("斗拱提交APP支付订单返回信息response:{}", JsonUtils.toJsonString(response));
            if (!"00000000".equals(response.get("resp_code"))) {
                channelPayOrderContext.setRemark("斗拱提交APP支付订单请求失败,失败信息" + response.get("resp_desc"));
                channelPayOrderContext.setProcessStatus(Boolean.FALSE);
                return ;
            }
            HuiFuPayOrderRequestParamsHandler.processSubmitOrderResultData(response, channelPayOrderContext);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("支付提交APP支付订单请求失败 message:{}", e.getMessage(), e);
            channelPayOrderContext.setRemark("斗拱提交APP支付订单请求失败,失败信息" + e.getMessage());
            channelPayOrderContext.setProcessStatus(Boolean.FALSE);
        }
    }

    @Override
    public void queryChannelPayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        commonQueryChannelPayOrder(channelPayOrderQueryContext);
    }

    @Override
    public void closeChannelPayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        commonCloseChannelPayOrder(channelPayOrderQueryContext);
    }

    @Override
    public void queryChannelClosePayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        commonQueryChannelClosePayOrder(channelPayOrderQueryContext);
    }

    @Override
    public void refundChannelPayOrder(ChannelRefundPayOrderContext channelRefundPayOrderContext) {
        commonRefundChannelPayOrder(channelRefundPayOrderContext);
    }

    @Override
    public void queryRefundChannelPayOrder(ChannelRefundPayOrderContext channelRefundPayOrderContext) {
        commonQueryRefundChannelPayOrder(channelRefundPayOrderContext);
    }
}
